package com.hsd.znsh.utils;

import com.alibaba.fastjson.JSONObject;
import com.apicloud.sdk.api.Push;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.http.HttpMethod;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

public class FileUtil {

	/*
	 * 获取文件后缀
	 */
	public static String getFileExt(String fileName){
		int dotIndex = fileName.lastIndexOf(".");
		String fileExt = fileName.substring(dotIndex + 1);
		return fileExt;
	}

	/**
	 * 删除文件夹里面的所有文件
	 *
	 * @param path
	 *            文件夹路径 如 c:/fqf
	 */
	public static void delAllFile(String path) {
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		if (!file.isDirectory()) {
			return;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (path.endsWith(File.separator)) {
				temp = new File(path + tempList[i]);
			} else {
				temp = new File(path + File.separator + tempList[i]);
			}
			if (temp.isFile()) {
				temp.delete();
			}
			if (temp.isDirectory()) {
				delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
				delFolder(path + "/" + tempList[i]);// 再删除空文件夹
			}
		}
	}

	/**
	 * 删除文件夹
	 *
	 * @param folderPath
	 *            文件夹路径及名称 如c:/fqf
	 */
	public static void delFolder(String folderPath) {
		try {
			delAllFile(folderPath); // 删除完里面所有内容
			String filePath = folderPath;
			filePath = filePath.toString();
			File myFilePath = new File(filePath);
			myFilePath.delete(); // 删除空文件夹

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/**
	 * 复制单个文件
	 *
	 * @param oldPath
	 *            源文件路径
	 * @param newPath
	 *            复制后路径
	 * @return 文件大小
	 */
	public static int copyFile(String oldPath, String newPath) {
		try {
			int bytesum = 0;
			int byteread = 0;
			File oldfile = new File(oldPath);
			if (oldfile.exists()) {
				InputStream inStream = new FileInputStream(oldPath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(newPath);
				byte[] buffer = new byte[1444];
				int length;
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					System.out.println(bytesum);
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.close();
			}
			return bytesum;
		} catch (Exception e) {
			System.out.println("复制单个文件操作出错");
			e.printStackTrace();
			return 0;
		}
	}

	/**
	 * 复制文件流到新的文件
	 *
	 * @param inStream
	 *            文件流
	 * @param file
	 *            新文件
	 * @return 是否复制成功
	 */
	public static boolean copyInputStreamToFile(final InputStream inStream, File file) throws IOException {
		int bytesum = 0;
		int byteread = 0;
		byte[] buffer = new byte[1024];
		FileOutputStream fs = new FileOutputStream(file);
		while ((byteread = inStream.read(buffer)) != -1) {
			bytesum += byteread; // 字节数 文件大小
			fs.write(buffer, 0, byteread);
		}
		inStream.close();
		fs.close();
		return true;
	}

	/**
	 * 删除指定路径下的文件
	 *
	 * @param filePathAndName
	 *            文件路径
	 */
	public static void delFile(String filePathAndName) {
		try {
			String filePath = filePathAndName;
			filePath = filePath.toString();
			File myDelFile = new File(filePath);
			myDelFile.delete();

		} catch (Exception e) {
			System.out.println("删除文件操作出错");
			e.printStackTrace();
		}
	}


	public static String copyToFile(MultipartFile multipartFile, String parentUrl) {
		try {
			File parentFile=new File(parentUrl);
			if(!parentFile.exists()) parentFile.mkdirs();
			SimpleDateFormat format=new SimpleDateFormat("yyyyMMddHHmmss");
			String filename=format.format(new Date())+ RandomStringUtils.randomNumeric(5)+"."+getFileExt(multipartFile.getOriginalFilename());
			File file=new File(parentFile,filename);
			multipartFile.transferTo(file);
			return parentUrl+"/"+filename;
		} catch (IOException e) {
			System.out.println("复制文件操作出错");
			e.printStackTrace();
			return "";
		}
	}

	public static void listDirectory(File directory){
		System.out.println(directory.getAbsolutePath());
		if(!directory.isDirectory()) return;
		if(directory.listFiles()!=null){
			for(File file:directory.listFiles()){
				if(file.isFile()){
					System.out.println(file.getAbsolutePath());
				}else{
					listDirectory(file);
				}
			}
		}
	}

	public static void listAllFile(){
		File[] files=File.listRoots();
		for(File file:files){
			if(file.isDirectory()){
				listDirectory(file);
			}else{
				System.out.println(file.getAbsolutePath());
			}

		}
	}



	public static void main(String[] args) throws DecoderException {
		/*SmsSingleSender sender = new SmsSingleSender(1400075102, "fc3e4823b658df68a7b4d443d10c3bdf");
		SmsSingleSenderResult result = null;
		try {
			String[] params = {"5678","1"};
			result = sender.sendWithParam("86", "15270292627",
					95718,params, "庆科信息", "","");
		} catch (HTTPException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.print(result);*/
		String cmd="YIDRngagzwAkVvir";
		System.out.println(new String(Base64.getDecoder().decode(cmd)));

	}
}
